//
//  BarCodeViewController.swift
//  iPhone
//
//  Created by Toj on 6/13/22.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class BarCodeViewController: STBaseViewController {
    
    override var pagePath: String {
        return "startimes://mydownload"
    }
    
    private lazy var barButton: UIButton = {
        let view = UIButton()
        view.setTitle("Bar", for: .normal)
        view.backgroundColor = .red
        view.addTarget(self, action: #selector(didTapBarcode), for: .touchUpInside)
        return view
    }()
    
    private lazy var qrButton: UIButton = {
        let view = UIButton()
        view.setTitle("Qr", for: .normal)
        view.backgroundColor = .init(hex: 0xE61919)
        view.addTarget(self, action: #selector(didTapQrcode), for: .touchUpInside)
        return view
    }()
    
    var startTime: CFAbsoluteTime = 0
    
    deinit {
        // let time = CFAbsoluteTimeGetCurrent() - startTime
        // print("-->toj: deinit time: \(time)")
    }
}

// MARK: - 生命周期
extension BarCodeViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("-->shantao: 1 class:\(st_className), func: \(#function)")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        
        print("-->shantao: 1 class:\(st_className), func: \(#function)")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        print("-->shantao: 1 class:\(st_className), func: \(#function)")
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        
        print("-->shantao: 1 class:\(st_className), func: \(#function)")
    }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension BarCodeViewController {
    
     private func didTapBarcode() {
        navigationController?.pushViewController(BarViewController(), animated: true)
    }
    
     private func didTapQrcode() {
        navigationController?.pushViewController(QrViewController(), animated: true)
    }
    
}

// MARK: - 子类可重写方法/ objc事件
@objc extension BarCodeViewController {
    
}

// MARK: - Public
extension BarCodeViewController {
    
}

// MARK: - Private
private extension BarCodeViewController {
    
    //写入本地
    func writeToLocal(image: UIImage,imgName: String) {
        guard let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return }
        let filePath = "\(path)/\(imgName).jpeg"
        guard let imgData = image.jpegData(compressionQuality: 0) else { return }
        let url = URL(fileURLWithPath: filePath)
        
        try? imgData.description.write(to: url, atomically: true, encoding: .utf8)
        
        // print("写入成功！！")
        // print("filePath=\(filePath)")
    }
}

// MARK: - 初始化设置
private extension BarCodeViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(barButton)
        
        view.addSubview(qrButton)
    }
    
    func setupMakeLayoutSubviews() {
        barButton.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        qrButton.frame = CGRect(x: 100, y: 300, width: 100, height: 100)
    }
    
    func setupMakeInitialize() {
        startTime = CFAbsoluteTimeGetCurrent()
        tt_isNavigationBarHidden = true
    }
}
